home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: jlilley@ix.netcom.com (John Lilley)
- Newsgroups: comp.lang.c++
- Subject: Re: Casting pointer to member function to pointer to function
- Date: 18 Mar 1996 18:03:06 GMT
- Organization: Netcom
- Message-ID: <4ik8gq$14k@reader2.ix.netcom.com>
- References: <314CD34A.7DBB@world.std.com>
- NNTP-Posting-Host: den-co8-14.ix.netcom.com
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-NETCOM-Date: Mon Mar 18 10:03:06 AM PST 1996
- X-Newsreader: WinVN 0.99.7
-
- In article <314CD34A.7DBB@world.std.com>, slide@world.std.com says...
- >
- >Hi all,
- >
- >I can't seem to get this to work and can't figure if it's even legal. I
- >have a global function that takes as an argument a pointer to function.
- >The pointer to function is defined as
- >
- >long (__stdcall *)(void *,unsigned int,unsigned int,long)
- >
- >I would like to pass a function that is a member of a class called CGWnd
- >to this function which would be defined as:
- >
- >long (__stdcall CGWnd::*)(void *,unsigned int,unsigned int,long)
- >
- >I've tried casting but but MSVC++ gives me an error that effectively
- >says I can't cast away the function's class membership. Anyone know if
- >there is a way around this?
-
- Yes, this is illegal. Member functions have an implicit "this"
- argument, You should not even attempt to write a function that
- emulates the "this" argument because compilers are free to choose
- a different calling convention for member functions.
-
- Instead, write a wrapper. This is ugly because you cannot associate
- the wrapper with a particular "A", so you must use a global "A".
- class A {
- long memberFunction(void* unsigned int, unsigned int, long);
- };
-
- // squirrel away some global A where you can get it...
- A *globalA:
- long wrapperFunction(void* vp, unsigned int ui1, unsigned int ui2, long l)
- {
- return globalA->memberFunction(vp, ui1, ui2, l);
- }
-
- However, in many cases of callback functions like this,
- the (void*) is a pointer to any data you want so you could use
- the "A" there and make it a lot cleaner.
-
- long wrapperFunction(void* vp, unsigned int ui1, unsigned int ui2, long l)
- {
- A* ap = (A*)vp;
- return ap->memberFunction(vp, ui1, ui2, l);
- }
-
- john lilley
-
-